package num66;

import java.util.Arrays;

/**
 * 66. 加一
 *
 * 题目： https://leetcode-cn.com/problems/plus-one/
 * 三叶姐题解： https://leetcode-cn.com/problems/valid-number/solution/gong-shui-san-xie-zi-fu-chuan-mo-ni-by-a-7cgc/
 *
 * me
 *
 */

class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        int res = digits[n - 1] + 1;
        if (res < 10) {
            digits[n - 1] = res;
            return digits;
        }

        for (int i = n - 1; i >= 0; i--) {

            digits[i] = res % 10;

            if (i - 1 < 0) {
                int[] ints = new int[n + 1];
                System.arraycopy(digits, 0, ints, 1, n);
                ints[0] += res / 10;
                return ints;
            }

            int t = digits[i - 1] += res / 10;
            if (t < 10) {
                break;
            }


        }
        return digits;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution().plusOne(new int[]{4,3,2,1})));
    }
}
